using QFramework.Example;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace QFramework
{
    public class UISlot : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
    {
        public Image Icon;
        public Text Count;
        public Slot Data;

        private bool isDragging = false;

        public UISlot InitWithData(Slot data)
        {
            Data = data;
            Data.OnChanged.Register(UpdateView)
                .UnRegisterWhenGameObjectDestroyed(gameObject);
            UpdateView();
            return this;
        }

        #region private method
        private void UpdateView()
        {
            if (Data.Count == 0)
            {
                Icon.Hide();
                Count.text = string.Empty;
            }
            else
            {
                Icon.sprite = Data.Item.GetIcon;
                Icon.Show();
                Count.text = Data.Count.ToString();
            }
        }
        private void SyncItemToMousePos()
        {
            var mousePos = Input.mousePosition;
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, mousePos, null, out var localPos))
            {
                Icon.LocalPosition2D(localPos);
            }
        }
        #endregion

        #region drag
        public void OnBeginDrag(PointerEventData eventData)
        {
            if (isDragging || Data.Count == 0) return;
            isDragging = true;

            var canvas = Icon.gameObject.AddComponent<Canvas>();
            canvas.overrideSorting = true;
            canvas.sortingOrder = 1000;
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (isDragging)
            {
                SyncItemToMousePos();
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (isDragging)
            {
                isDragging = false;
                Icon.GetComponent<Canvas>().DestroySelf();
                Icon.LocalPositionIdentity();

                if (ItemKit.CurrentSlotPointerOn)
                {
                    var uiSlot = ItemKit.CurrentSlotPointerOn;
                    var rectTransform = uiSlot.transform as RectTransform;
                    if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition))
                    {
                        if (uiSlot.Data.Group.CheckCondition(Data.Item))
                        {
                            var cachedItem = uiSlot.Data.Item;
                            var cacheCount = uiSlot.Data.Count;

                            uiSlot.Data.Item = Data.Item;
                            uiSlot.Data.Count = Data.Count;

                            Data.Item = cachedItem;
                            Data.Count = cacheCount;

                            Data.OnChanged.Trigger();
                            uiSlot.Data.OnChanged.Trigger();
                        }
                    }
                }
                else
                {
                    Data.Item = null;
                    Data.Count = 0;
                    Data.OnChanged.Trigger();
                }
            }
        }
        #endregion

        #region pointer
        public void OnPointerEnter(PointerEventData eventData)
        {
            UIItemTip.Show(this);
            ItemKit.CurrentSlotPointerOn = this;
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            UIItemTip.Hide();
            if (ItemKit.CurrentSlotPointerOn)
            {
                ItemKit.CurrentSlotPointerOn = null;
            }
        }
        #endregion
    }
}